<template>
  <div class="app-container">
    <!-- 搜索条件 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="新闻标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入新闻标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新闻分类" prop="newsCategory">
        <el-select v-model="queryParams.newsCategory" placeholder="请选择新闻分类" clearable>
          <el-option label="政策资讯" value="政策资讯" />
          <el-option label="行业动态" value="行业动态" />
          <el-option label="使用指南" value="使用指南" />
          <el-option label="信用知识" value="信用知识" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 新闻列表 -->
    <div class="news-list">
      <el-row :gutter="20">
        <el-col :span="24" v-for="news in newsList" :key="news.newsId">
          <el-card class="news-card" @click.native="readNews(news)">
            <div class="news-header">
              <div class="news-title">
                <h3>{{ news.title }}</h3>
                <el-tag type="info" size="mini">{{ news.newsCategory }}</el-tag>
              </div>
              <div class="news-meta">
                <span class="author">{{ news.author }}</span>
                <span class="publish-time">{{ parseTime(news.publishTime, '{y}-{m}-{d}') }}</span>
                <span class="read-count">阅读量：{{ news.readCount }}</span>
              </div>
            </div>
            <div class="news-summary">
              {{ news.summary }}
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新闻详情对话框 -->
    <el-dialog :title="selectedNews.title" :visible.sync="newsDetailVisible" width="800px" :before-close="handleDetailClose">
      <div v-if="selectedNews.newsId" class="news-detail">
        <div class="news-info">
          <span class="author">作者：{{ selectedNews.author }}</span>
          <span class="publish-time">发布时间：{{ parseTime(selectedNews.publishTime, '{y}-{m}-{d} {h}:{i}') }}</span>
          <span class="read-count">阅读量：{{ selectedNews.readCount }}</span>
          <span class="category">分类：{{ selectedNews.newsCategory }}</span>
        </div>
        <div class="news-content" v-html="selectedNews.content"></div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="newsDetailVisible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNews, readNews } from "@/api/credit/news";

export default {
  name: "CreditNews",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 新闻资讯表格数据
      newsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 新闻详情弹窗
      newsDetailVisible: false,
      // 选中的新闻
      selectedNews: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        newsCategory: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询新闻资讯列表 */
    getList() {
      this.loading = true;
      listNews(this.queryParams).then(response => {
        this.newsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        newsId: null,
        title: null,
        content: null,
        summary: null,
        author: null,
        newsCategory: null,
        readCount: null,
        publishStatus: null,
        publishTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 阅读新闻 */
    readNews(news) {
      readNews(news.newsId).then(response => {
        this.selectedNews = response.data;
        this.newsDetailVisible = true;
        // 更新列表中的阅读量
        const index = this.newsList.findIndex(item => item.newsId === news.newsId);
        if (index !== -1) {
          this.newsList[index].readCount = response.data.readCount;
        }
      });
    },
    /** 关闭详情弹窗 */
    handleDetailClose() {
      this.newsDetailVisible = false;
      this.selectedNews = {};
    }
  }
};
</script>

<style scoped>
.news-list {
  margin-top: 20px;
}

.news-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s;
}

.news-card:hover {
  box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.15);
}

.news-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 15px;
}

.news-title {
  flex: 1;
}

.news-title h3 {
  margin: 0 0 8px 0;
  color: #303133;
  font-size: 18px;
  font-weight: 600;
}

.news-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  font-size: 12px;
  color: #909399;
}

.news-meta span {
  margin-bottom: 4px;
}

.news-summary {
  color: #606266;
  line-height: 1.6;
  font-size: 14px;
}

.news-detail {
  max-height: 600px;
  overflow-y: auto;
}

.news-info {
  padding: 15px 0;
  border-bottom: 1px solid #EBEEF5;
  margin-bottom: 20px;
}

.news-info span {
  margin-right: 20px;
  font-size: 12px;
  color: #909399;
}

.news-content {
  line-height: 1.8;
  color: #303133;
}

.news-content >>> h1,
.news-content >>> h2,
.news-content >>> h3,
.news-content >>> h4,
.news-content >>> h5,
.news-content >>> h6 {
  color: #303133;
  margin: 20px 0 10px 0;
}

.news-content >>> p {
  margin: 15px 0;
  text-indent: 2em;
}

.news-content >>> img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 20px auto;
}
</style> 